package com.my.controller;


import ch.qos.logback.core.net.SyslogOutputStream;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.my.bean.*;
import com.my.bean.File;
import com.my.service.BabyNursingService;
import com.my.util.FileUpload;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.io.*;
import java.util.*;

/**
 * <p>
 * 宝宝护理记录 前端控制器
 * </p>
 *
 * @author 闫亚茹
 * @since 2018-08-24
 */
@Controller
@RequestMapping("/babyNursing")
public class BabyNursingController {
	@Autowired
    BabyNursingService babyNursingService;
    Logger logger=Logger.getLogger(WorkguidaceController.class);
	//添加
    @RequestMapping("/addbaByNursing")
    @ResponseBody
    public  String addBaby(String param,HttpSession session){
        BabyNursing babyNursing= JSON.parseObject(param,BabyNursing.class);
        babyNursing.setIsDelete(2);
        babyNursing.setCreateTime(new Date().getTime());
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        babyNursing.setAreaid(areaid);
        if (babyNursingService.insert(babyNursing)){
            logger.info("{addbaByNursing|保存成功}");
            return  "保存成功";
        }else{
            logger.info("{addbaByNursing|保存失败}");
            return  "保存失败";
        }
}

    /**
     * 查询并分页
     * @param voPage
     * @return
     */
    @RequestMapping("/selectBabyNursing")
    @ResponseBody
    public ResultPage <BabyNursing> selectBabyNursing(String lin, String data, VoPage voPage, HttpSession session){
        boolean admin=false;
//如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map=new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<BabyNursing> resultPage = babyNursingService.selectMyPage(map,voPage,areaid,admin);
        return resultPage;
    }
    @RequestMapping("/Baby")
    public String Baby (){
        return "payment_baby";
 }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delectBaby")
    @ResponseBody
    public  boolean delectbaby(Long id){
        return  babyNursingService.deleteById(id);
    }

    /**
     * 修改
     * @param
     * @return
     */
    @RequestMapping("/updatebaby")
    @ResponseBody
    public  String updatebaby(String param){
        BabyNursing  babyNursing = JSONObject.parseObject(param,BabyNursing.class);
        if(babyNursingService.updateById(babyNursing)){
            return  "修改成功";
        }else{
            return "修改失败";
        }
    }

    @RequestMapping("/baby_add")
    public String babyAdd(){
        return "/Alert/baby_add";

    }
   @RequestMapping("/baby_edit")
    public String babyEdit(){
        return "/Alert/baby_edit";

    }

    /**
     * 跳转上传文件页面
     * @return
     */
    @RequestMapping("file1")
    public String file(){
        return "/file1";
    }


    /**
     * 实现文件上传  单个文件
     * */
    @RequestMapping("fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam("fileName") MultipartFile file) throws FileNotFoundException {
       return FileUpload.fileUpload(file);
    }


    /**
     * 文件下载
     * @param response
     * @return
     */
    @RequestMapping("download")
    public String downLoad(HttpServletResponse response) throws UnsupportedEncodingException {
        com.my.bean.File file=new File();
        file.setUrl("C:\\Users\\Zhang\\Desktop\\月子项目\\YueZi\\YueZi\\uploads\\1538968262053无标题.png");
return  FileUpload.downLoad(response,file);
    }





}
